import api from '@/api'


const music = {
    namespaced: true,
    state: {
        playUrl: 'http://music.163.com/song/media/outer/url?id=',
        playId: '',
        playInfo: null,
        playState: false,
        playIndex: 0,
        cacheList: [],
        playList: [],
        playMode: 'repeat'  
    },
    mutations: {
        SET_PLAY_URL(state, url) {
            state.playUrl = url;
        },
        SET_PLAY_ID(state, id) {
            state.playId = id;
        },
        SET_PLAY_INFO(state, info) {
            state.playInfo = info
        },
        SET_PLAY_STATE(state, ps) {
            state.playState = ps;
        },
        SET_CACHE_LIST(state, list) {

            let caches = state.cacheList;

            let cids = caches.map(i=> i.id);
            
            list = list.filter(i => !cids.includes(i.id));

            caches = caches.concat(list);

            if(caches.length >= 20){

                caches =  caches.slice(caches.length - 20);
            }

            state.cacheList = caches;

            localStorage.setItem('cacheList', JSON.stringify(caches));
        },
        SET_PLAY_LIST(state, list) {
            state.playList = list;
        },
        SET_PLAY_INDEX(state, index) {
            state.playIndex = index;
        },
        SET_PLAY_MODE(state, mode) {
            state.playMode = mode;
        }
    },
    actions: {
        setPlayUrl({commit}, url) {
            commit('SET_PLAY_URL', url);
        },

        setPlayId({commit, dispatch}, id) {
            
            commit('SET_PLAY_ID', id);
            return api.getMusicUrl(id).then(res => {

                let url = res.data.data[0].url;
                
                dispatch('setPlayUrl', url) 
            })

        },

         setPlayInfo({commit, dispatch, state}, id) {

            // console.log(state.cacheList);

            if(state.cacheList.length === 0){
                let cache = localStorage.getItem('cacheList');
                if(cache){
                    cache = JSON.parse(cache);
                    commit('SET_CACHE_LIST', cache);
                }
            }

            let has = state.cacheList.find(i => i.id === id);
            if(has){
                // 有缓存

                commit("SET_PLAY_INFO", has);
                return dispatch("setPlayId", has.id).then(() => {
                    return has;
                });
            }else{
                // 无缓存
                return api.getSongInfo(id).then((res)=>{
                    let info = res.data.songs[0];
                    dispatch('setCacheList', [info]);
                    commit('SET_PLAY_INFO', info);
                    
                    return dispatch('setPlayId', info.id).then(() => {
                        return info;
                    });
                })
            }

            
        },

        setPlayState({ commit }, ps) {
            commit('SET_PLAY_STATE', ps)
        },


        setCacheList({ commit }, list){
            commit("SET_CACHE_LIST", list);
        },

        setPlayList({ commit }, list) {
            commit('SET_PLAY_LIST', list);
        },

        setPlayIndex({ commit }, index) {
            commit('SET_PLAY_INDEX', index)
        },

        setPlayMode({ commit } , mode) {
            commit('SET_PLAY_MODE', mode);
        }
    },
    getters: {
        playUrl: state =>  state.playUrl,
        playId: state =>  state.playId,
        playInfo: state => state.playInfo,
        playState: state => state.playState,
        cacheList: state => state.cacheList,
        playList: state => state.playList,
        playIndex: state => state.playIndex,
        playMode: state => state.playMode
    }
}

export default music;